package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.DeleteException;
import com.itheima.reggie.common.R;

import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealContorller {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealDishService setMealDishService;

    @PostMapping
    public R addTaoCan(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息:{}" + setmealDto);
        setMealService.addTanJia(setmealDto);

        return R.success("添加套餐成功");
    }


    @GetMapping("/page")
    public R AllChaXunTaoCan(Integer page, Integer pageSize, String name) {
        //分页构造对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> dto = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件 根据name进行like模糊查询
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //更新时间
        setMealService.page(setmealPage, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(setmealPage, dto, "records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据id查询对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dto.setRecords(list);

        //返回更新后的数据
        return R.success(dto);
    }


    @DeleteMapping
    public R deleteTaoCan(@RequestParam List<Long> ids) {

        setMealService.deleteShan(ids);

        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R updateShouMai(@RequestParam List<Long> ids, Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        setMealService.update(setmeal, wrapper);
        return R.success("修改状态成功!");
    }

    @GetMapping("/{id}")
    public R updateId(@PathVariable Long id){
        SetmealDto setmealDto = setMealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setMealService.updateWithFlavor(setmealDto);
        return R.success("修改套餐成功");
    }


    @GetMapping("/list")
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus()!= null,Setmeal::getStatus,setmeal.getStatus());
        qw.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setMealService.list(qw);
        return R.success(list);
    }
}
